package com.cam.generator.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cam.generator.dto.GenCodeFieldDTO;
import com.cam.generator.entity.GenCodeField;
import com.cam.generator.mapStruct.GenCodeFieldMapStruct;
import com.cam.generator.mapper.GenCodeFieldMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class GenCodeFieldService extends ServiceImpl<GenCodeFieldMapper, GenCodeField> {

    private final GenCodeFieldMapStruct mapStruct;

    public GenCodeFieldService(GenCodeFieldMapStruct mapStruct) {
        this.mapStruct = mapStruct;
    }

    public void removeByGenId(String genId) {
        /*LambdaQueryWrapper<GenCodeField> removeWrapper = new LambdaQueryWrapper<>();
        removeWrapper.eq(GenCodeField::getGenId, genId);
        super.remove(removeWrapper);*/
        baseMapper.removeRealByGenId(genId);
    }

    public void saveOrUpdateList(List<GenCodeFieldDTO> dtoList, String genId) {
        List<GenCodeField> entityList = dtoList.stream().map(eachDTO -> {
            GenCodeField eachEntity = mapStruct.toEntity(eachDTO);
            eachEntity.setGenId(genId);
            return eachEntity;
        }).collect(Collectors.toList());
        super.saveOrUpdateBatch(entityList);
    }

    public List<GenCodeFieldDTO> findList(String genId) {
        QueryWrapper<GenCodeField> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gen_id", genId);
        List<GenCodeField> entityList = baseMapper.selectList(queryWrapper);
        return entityList.stream().map(mapStruct::toDTO).collect(Collectors.toList());
    }

}
